package ge.telasi.tasks.ui.search;

import ge.telasi.tasks.search.Condition;
import ge.telasi.tasks.ui.UIUtils;
import java.util.Calendar;
import javax.swing.Icon;

/**
 * @author dimitri
 */
public class DateField extends Field {
    public static final int EXACT = 0;
    public static final int INTERVAL = 1;

    public static final int BY_DATE = 0;
    public static final int BY_COUNT = 1;

    private int method = EXACT;
    private int subMethod = BY_DATE;
    private Calendar date1;
    private Calendar date2;
    private int count1;
    private int count2;

    public DateField(String name, String humanName) {
        super(name, humanName);
    }

    public int getMethod() {
        return method;
    }

    public void setMethod(int method) {
        this.method = method;
    }

    public int getSubMethod() {
        return subMethod;
    }

    public void setSubMethod(int subMethod) {
        this.subMethod = subMethod;
    }
    
    public Calendar getDate1() {
        return date1;
    }

    public void setDate1(Calendar date1) {
        this.date1 = date1;
    }

    public Calendar getDate2() {
        return date2;
    }

    public void setDate2(Calendar date2) {
        this.date2 = date2;
    }

    public int getCount1() {
        return count1;
    }

    public void setCount1(int count1) {
        this.count1 = count1;
    }

    public int getCount2() {
        return count2;
    }

    public void setCount2(int count2) {
        this.count2 = count2;
    }
    
    @Override
    public String getType() {
        return "date";
    }

    @Override
    public Icon getIcon() {
        return UIUtils.getIcon16x16("date.png");
    }

    @Override
    public Condition getCondition() {
        Condition condition = null;
        if (method == EXACT) {
            condition = generateEquality();
        } else {
            condition = generateInterval();
        }
        return condition;
    }

    private Condition generateEquality() {
        if (subMethod == BY_DATE && date1 == null)
            return null;

        Calendar d1 = null;
        if (subMethod == BY_DATE) {
            d1 = this.date1;
        } else {
            d1 = Calendar.getInstance();
            d1.add(Calendar.DAY_OF_MONTH, count1);
        }

        // field
        String fieldName = getNextField();
        
        // where condition
        StringBuilder b = new StringBuilder();
        b.append(" ( ");
        b.append(getSysName());
        b.append(" = ");
        b.append(":");
        b.append(fieldName);
        b.append(" ) ");
        String where = b.toString();

        // substitution
        Calendar fieldValue = d1;

        // build condition
        Condition condition = new Condition();
        condition.setCondition(where);
        condition.getSubstitutions().put(fieldName, fieldValue);

        return condition;
    }

    private Condition generateInterval() {
        boolean d1valuable = subMethod == BY_COUNT || date1 != null;
        boolean d2valuable = subMethod == BY_COUNT || date2 != null;

        if (!d1valuable && !d2valuable)
            return null;
        
        String f1 = d1valuable ? getNextField() : null;
        String f2 = d2valuable ? getNextField() : null;

        Calendar d1 = null;
        Calendar d2 = null;
        if (subMethod == BY_DATE) {
            d1 = this.date1;
            d2 = this.date2;
        } else {
            d1 = Calendar.getInstance();
            d2 = Calendar.getInstance();
            d1.add(Calendar.DAY_OF_MONTH, count1);
            d2.add(Calendar.DAY_OF_MONTH, count2);
        }

        // where condition
        StringBuilder b = new StringBuilder();
        b.append(" ( ");
        if (d1valuable) {
            b.append(getSysName());
            b.append(" >= ");
            b.append(":");
            b.append(f1);
        }
        if (d1valuable && d2valuable) {
            b.append(" AND ");
        }
        if (d2valuable) {
            b.append(getSysName());
            b.append(" <= ");
            b.append(":");
            b.append(f2);
        }
        b.append(" ) ");
        String where = b.toString();

        // substitution
        Calendar f1value = d1;
        Calendar f2value = d2;

        // build condition
        Condition condition = new Condition();
        condition.setCondition(where);
        if (d1valuable) {
            condition.getSubstitutions().put(f1, f1value);
        }
        if (d2valuable) {
            condition.getSubstitutions().put(f2, f2value);
        }

        return condition;
    }

}
